home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / memory < prev    next >
Encoding:
Text File  |  2000-06-08  |  3.4 KB  |  135 lines

  1. /*
  2.  * Copyright (c) 1997-1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Silicon Graphics makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  *
  13.  */
  14.  
  15. #ifndef __SGI_STL_MEMORY
  16. #define __SGI_STL_MEMORY
  17.  
  18. #include <stl_algobase.h>
  19. #include <stl_alloc.h>
  20. #include <stl_construct.h>
  21. #include <stl_tempbuf.h>
  22. #include <stl_uninitialized.h>
  23. #include <stl_raw_storage_iter.h>
  24.  
  25.  
  26. __STL_BEGIN_NAMESPACE
  27.  
  28. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
  29.     defined(__STL_MEMBER_TEMPLATES)
  30.  
  31. template<class _Tp1> struct auto_ptr_ref {
  32.   _Tp1* _M_ptr;
  33.   auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
  34. };
  35.  
  36. #endif
  37.  
  38. template <class _Tp> class auto_ptr {
  39. private:
  40.   _Tp* _M_ptr;
  41.  
  42. public:
  43.   typedef _Tp element_type;
  44.  
  45.   explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}
  46.   auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
  47.  
  48. #ifdef __STL_MEMBER_TEMPLATES
  49.   template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
  50.     : _M_ptr(__a.release()) {}
  51. #endif /* __STL_MEMBER_TEMPLATES */
  52.  
  53.   auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
  54.     if (&__a != this) {
  55.       delete _M_ptr;
  56.       _M_ptr = __a.release();
  57.     }
  58.     return *this;
  59.   }
  60.  
  61. #ifdef __STL_MEMBER_TEMPLATES
  62.   template <class _Tp1>
  63.   auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
  64.     if (__a.get() != this->get()) {
  65.       delete _M_ptr;
  66.       _M_ptr = __a.release();
  67.     }
  68.     return *this;
  69.   }
  70. #endif /* __STL_MEMBER_TEMPLATES */
  71.  
  72.   // Note: The C++ standard says there is supposed to be an empty throw
  73.   // specification here, but omitting it is standard conforming.  Its 
  74.   // presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
  75.   // this is prohibited.
  76.   ~auto_ptr() { delete _M_ptr; }
  77.  
  78.   _Tp& operator*() const __STL_NOTHROW {
  79.     return *_M_ptr;
  80.   }
  81.   _Tp* operator->() const __STL_NOTHROW {
  82.     return _M_ptr;
  83.   }
  84.   _Tp* get() const __STL_NOTHROW {
  85.     return _M_ptr;
  86.   }
  87.   _Tp* release() __STL_NOTHROW {
  88.     _Tp* __tmp = _M_ptr;
  89.     _M_ptr = 0;
  90.     return __tmp;
  91.   }
  92.   void reset(_Tp* __p = 0) __STL_NOTHROW {
  93.     if (__p != _M_ptr) {
  94.       delete _M_ptr;
  95.       _M_ptr = __p;
  96.     }
  97.   }
  98.  
  99.   // According to the C++ standard, these conversions are required.  Most
  100.   // present-day compilers, however, do not enforce that requirement---and, 
  101.   // in fact, most present-day compilers do not support the language 
  102.   // features that these conversions rely on.
  103.   
  104. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
  105.     defined(__STL_MEMBER_TEMPLATES)
  106.  
  107. public:
  108.   auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
  109.     : _M_ptr(__ref._M_ptr) {}
  110.  
  111.   auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
  112.     if (__ref._M_ptr != this->get()) {
  113.       delete _M_ptr;
  114.       _M_ptr = __ref._M_ptr;
  115.     }
  116.     return *this;
  117.   }
  118.  
  119.   template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW 
  120.     { return auto_ptr_ref<_Tp1>(this->release()); }
  121.   template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
  122.     { return auto_ptr<_Tp1>(this->release()); }
  123.  
  124. #endif /* auto ptr conversions && member templates */
  125. };
  126.  
  127. __STL_END_NAMESPACE
  128.  
  129. #endif /* __SGI_STL_MEMORY */
  130.  
  131.  
  132. // Local Variables:
  133. // mode:C++
  134. // End:
  135.